package com.ambulance.project.biz.controller.treatweb;

import com.ambulance.common.exception.CustomException;
import com.ambulance.common.utils.ResultMapUtil;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.domain.AjaxResultData;
import com.ambulance.project.biz.dto.HsDto;
import com.ambulance.project.biz.entity.Car;
import com.ambulance.project.biz.entity.Dispatch;
import com.ambulance.project.biz.entity.DispatchCase;
import com.ambulance.project.biz.entity.Patient;
import com.ambulance.project.biz.service.CarService;
import com.ambulance.project.biz.service.DispatchCaseService;
import com.ambulance.project.biz.service.DispatchService;
import com.ambulance.project.biz.service.PatientService;
import com.ambulance.project.manager.utils.DoctorAndNurseCommonUtil;
import com.ambulance.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author RXL
 * @Date 2020/6/16 11:13
 * @Version 1.0
 */
@Api(tags = {"会诊子系统Web-病历相关接口"})
@RestController
@RequestMapping(value = "/treat/web/v1/patient", method = RequestMethod.POST)
public class TreatWebPatientController {
    @Autowired
    private DoctorAndNurseCommonUtil doctorAndNurseCommonUtil;
    @Autowired
    private DispatchCaseService dispatchCaseService;
    @Autowired
    private DispatchService dispatchService;
    @Autowired
    private PatientService patientService;
    @Autowired
    private CarService carService;

    @RequestMapping(value = "/case/info")
    @ResponseBody
    @ApiOperation(value = "电子病历-普通病历-患者信息", notes = "电子病历-普通病历-患者信息", produces = "application/json")
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    /**
     * 获取调度中的病历的初始信息
     */
    public Object getDispathcCaseNewinfo(@RequestParam(value = "dispathchID") Integer dispatchID) {
        final QueryWrapper<DispatchCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dispatchID", dispatchID);
        final DispatchCase one = dispatchCaseService.getOne(queryWrapper);
        if (CommonUtils.checkNull(one)) {
            throw new CustomException("无效的调度ID");
        }
        String[] param = {"patient_name", "patient_sex", "patient_age", "patient_mobile", "patient_address", "hospital", "major_case", "base_help",
                "treatment_record", "medicine_sensitive", "nhiss_score", "gcs_score", "treat_measure", "medicine", "form_status", "hospitalID"};
        final Map<String, Object> stringObjectMap = ResultMapUtil.customerObjField(one, param);
        return AjaxResult.success(stringObjectMap);
    }

    @RequestMapping(value = "/dispatchCase/info")
    @ResponseBody
    @ApiOperation(value = "电子病历-普通病历-病历详情", notes = "电子病历-普通病历-病历详情", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "房间id", dataType = "int", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    /**
     * 获取调度中的病历的初始信息
     */
    public AjaxResult getDispathcCaseBYDoctorId(@RequestParam(value = "id", required = true) Integer id) {
        // id 可能是医生的也可能是护士的医生范围1-10 000 000
        Integer dispatchID;
        if (id < 10000000) {
            //医生
            final Dispatch one = dispatchService.getOne(new QueryWrapper<Dispatch>().eq("doctorID", id).orderByDesc("receive_help_time").last("LIMIT 1"));
            if (CommonUtils.checkNull(one)) {
                throw new CustomException("未查到相关信息");
            }
            dispatchID = one.getDispatchID();
        } else {
            //护士
            final Dispatch one = dispatchService.getOne(new QueryWrapper<Dispatch>().eq("nurseID", id).orderByDesc("receive_help_time").last("LIMIT 1"));
            if (CommonUtils.checkNull(one)) {
                throw new CustomException("未查到相关信息");
            }
            dispatchID = one.getDispatchID();
        }

        final HashMap<String, Object> map = Maps.newHashMap();
        final QueryWrapper<DispatchCase> queryWrapper1 = new QueryWrapper<>();
        String[] param1 = {"dispatch_caseID", "patient_name", "patient_sex", "patient_age", "patient_mobile", "patient_address", "major_case", "base_help",
                "treatment_record", "medicine_sensitive", "nhiss_score", "gcs_score", "treat_measure", "medicine", "hospital", "patient_id", "dispatchID"};
        queryWrapper1.eq("dispatchID", dispatchID).select(param1);
        final DispatchCase byId = dispatchCaseService.getOne(queryWrapper1);
        if (CommonUtils.checkNull(byId)) {
            throw new CustomException("无效的参数");
        }
        map.put("dispatchCase", ResultMapUtil.customerObjField(byId, param1));
        final QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        String[] param = {"name", "phone", "sex", "age", "patient_local", "patient_card", "check_result"};
        queryWrapper.select(param).eq("patientId", byId.getPatientId());
        final Patient patientServiceById = patientService.getOne(queryWrapper);
        map.put("patient120", ResultMapUtil.customerObjField(patientServiceById, param));
        return AjaxResult.success(map);
    }

    @RequestMapping(value = "/philips")
    @ResponseBody
    @ApiOperation(value = "超声信息", notes = "超声信息", produces = "application/json")
    @ApiResponses({
            @ApiResponse(code = 200, response = HsDto.class, message = "响应")
    })
    /**
     * 获取调度中超声信息
     */
    public Object getPhilips(@RequestParam(value = "dispathchID") Integer dispatchID) {
        // 根据调度id查汇声info
        final Dispatch byId = dispatchService.getById(dispatchID);
        final HsDto hsDto = new HsDto();
        hsDto.setHsStatus(byId.getHsStatus());
        hsDto.setHsRtmp(byId.getHsRtmp());
        final Car carInfo = carService.getById(byId.getCarID());
        hsDto.setRoomID(carInfo.getUltrasonicRoom());
        return AjaxResult.success(hsDto);
    }
}
